//
//  TrendQueueViewController.m
//  HappyTrip
//
//  Created by dlios on 15-3-28.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TrendQueueViewController.h"
#import "UIImageView+AFNetworking.h"
#import "TripsQueue.h"
#import "TripsQueueCell.h"
#import "TravelNoteViewController.h"
// 刷新加载
#import "MJRefresh.h"


@interface TrendQueueViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *tripsArr;
@property (nonatomic, retain) UITableView *tableView;
// 上拉下拉tabBar出现
@property (nonatomic, assign) CGFloat offY;
@property (nonatomic ,retain) UIScrollView *scrollView;

@end

@implementation TrendQueueViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:170.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
//    self.tabBarController.tabBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.title = @"热门游记";
    [self handleData:0];
    self.tripsArr = [NSMutableArray array];
  
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.tableView addGestureRecognizer:swipe];
    [self.view addSubview:self.tableView];
    [swipe release];
    [_tableView release];
    
    // 上拉下拉tabBar出现
    self.scrollView = [[UIScrollView alloc] init];
//    self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.tableView.frame.size.height);
    self.scrollView.delegate = self;
    [self.tableView addSubview:self.scrollView];
    [_scrollView release];
    [self.tableView registerClass:[TripsQueueCell class] forCellReuseIdentifier:@"reuse"];
    
    // 菊花
    self.MBProgress = [[MBProgressHUD alloc] initWithView:self.view];
    self.MBProgress.labelText = @"加载中...";
    self.MBProgress.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:self.MBProgress];
    [self.MBProgress show:YES];
    [_MBProgress release];
    
    //刷新加载
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
//        self.tripsArr = [NSMutableArray array];
//        [self handleData:0];
        [self.tableView.header endRefreshing];

    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        static NSInteger i = 1;
        [self handleData:i++ * 20];
    }];

    
}
// tableView的点击方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tripsArr.count;
}
// tableView的协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TripsQueue *tripsQueue = [self.tripsArr objectAtIndex:indexPath.row];
    TripsQueueCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.tripsQueue = tripsQueue;
    return cell;
}
// tableView的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.view.frame.size.height + 30) / 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TripsQueue *tripsQueue = [self.tripsArr objectAtIndex:indexPath.row];
    TravelNoteViewController *travVC = [[TravelNoteViewController alloc] init];
    travVC.transmitId = tripsQueue.Id;
    [self.navigationController pushViewController:travVC animated:YES];
    travVC.hidesBottomBarWhenPushed = YES;
    [travVC release];
}
- (void)handleData:(NSInteger)startNumber
{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/hot/?start=%ld&count=20&is_ipad=true", startNumber];
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [NSString stringWithFormat:@"%@/%ld.happyTripDestination", docPath, [urlStr hash]];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        BOOL result = [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
        NSLog(@"存储成功: %d", result);
        NSLog(@"%@", path);
        NSDictionary *array = [responseObject objectForKey:@"trips"];
        for (NSDictionary *dic in array) {
            TripsQueue *tripsQueue = [[TripsQueue alloc] initWithDictionary:dic];
            [self.tripsArr addObject:tripsQueue];
            [tripsQueue release];
//            NSLog(@"self.tripsArr %@", self.tripsArr);
        }
        [self.tableView reloadData];
        //刷新加载
        [self.tableView.footer endRefreshing];
        [self.MBProgress removeFromSuperview];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        id resultID = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        //        self.collectionViewMutableArr = [resultID objectForKey:@"elements"];
        NSLog(@"result: %@", resultID);
        [self.MBProgress removeFromSuperview];

            }];
    //    [self.tableView reloadData];
}
// 上啦下拉tabBar出现消失
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    // 上划时标签栏下潜
//    NSLog(@"%lf", self.offY);
//    if (scrollView.contentOffset.y > 0 && self.tabBarController.tabBar.frame.origin.y <= self.view.frame.size.height)  {
//        [UIView animateWithDuration:1 animations:^{
//            for (NSInteger i = 1; i < 6; i++) {
//                self.tabBarController.tabBar.frame = CGRectMake(0, self.view.frame.size.height - 49 + 9.8 * i , self.view.frame.size.width, 49);
//            }
//        }];
//    }
//    if (scrollView.contentOffset.y < 0) {
////        self.imageView.frame = CGRectMake(0 + self.scrollView.contentOffset.y / 2, 64 + self.scrollView.contentOffset.y, self.view.frame.size.width - self.scrollView.contentOffset.y, self.view.frame.size.width / 1.8 - self.scrollView.contentOffset.y);
////        self.wordLabel.frame = CGRectMake(-self.scrollView.contentOffset.y / 2, self.view.frame.size.width / 1.8 - self.scrollView.contentOffset.y - self.view.frame.size.width / 7.2, self.imageView.frame.size.width, self.view.frame.size.width / 7.2);
//    }
//    // 下滑时标签栏出现
//    if (self.offY > scrollView.contentOffset.y ) {
//        [UIView animateWithDuration:1 animations:^{
//            for (NSInteger i = 1; i < 6; i++) {
//                self.tabBarController.tabBar.frame = CGRectMake(0, self.view.frame.size.height - 9.8 * i , self.view.frame.size.width, 49);
//            }
//        }];
//    }
//    // 滑到底时标签栏消失
//    if ((scrollView.contentOffset.y - (scrollView.contentSize.height - self.view.frame.size.height) < 10) && (scrollView.contentOffset.y - (scrollView.contentSize.height - self.view.frame.size.height) > -10) && self.tabBarController.tabBar.frame.origin.y <= self.view.frame.size.height) {
//        [UIView animateWithDuration:1 animations:^{
//            //            self.tabBarController.tabBar.hidden = YES;
//            for (NSInteger i = 1; i < 6; i++) {
//                self.tabBarController.tabBar.frame = CGRectMake(0, self.view.frame.size.height - 49 + 9.8 * i , self.view.frame.size.width, 49);
//            }
//        }];
//        //        self.offY = self.scrollView.contentOffset.y;
//    }
//    self.offY = scrollView.contentOffset.y;
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
